package aliyun

import (
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

//阿里云相关参数
type AliYun struct {
	MendInt        string `validate:"required"`
	Ak             string `validate:"required"`
	Sk             string `validate:"required"`
	BucketName     string `validate:"required"`
	UploadFilePath string `validate:"required"`
	LocalFilePath  string `validate:"required"`
}

// 是否需要帮助参数，默认关闭
var (
	Help bool = false
)

func NewAliYun(m, a, s, b, u, l string) *AliYun {
	return &AliYun{
		MendInt:        m,
		Ak:             a,
		Sk:             s,
		BucketName:     b,
		UploadFilePath: u,
		LocalFilePath: l,
	}
}

func (a *AliYun) UploadFile() error {
	client, err := oss.New(a.MendInt, a.Ak, a.Sk)
	if err != nil {
		return fmt.Errorf("error： %v,Suspected parameter error\n,", err)
	}

	bucket, err := client.Bucket(a.BucketName)
	if err != nil {
		return fmt.Errorf("error： %v,BucketName error\n,", err)
	}
	err = bucket.PutObjectFromFile(a.UploadFilePath, a.LocalFilePath)
	if err != nil {
		return fmt.Errorf("error： %v, post file is not valid\n,", err)
	}

	signedURL, err := bucket.SignURL(a.UploadFilePath, oss.HTTPGet, 60*60*24)
	if err != nil {
		return fmt.Errorf("sign file download url error, %s", err)
	}

	fmt.Printf("下载链接: %s\n", signedURL)
	fmt.Println("\n注意: 文件下载有效期为1天, 中转站保存时间为3天, 请及时下载")
	return nil
}
